package com.example.home.house.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.house.adapter.SearchhousingAdapter;
import com.example.home.house.bean.HouseBean;
import com.example.home.tool.OKHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class HouseActivity extends AppCompatActivity {
    private ImageView back;
    private Button butHouse;
    private TabLayout tabList;
    private RecyclerView rcvList;
    private SearchView house_search;
    private HouseBean houseBean;
    private SearchhousingAdapter searchhousingAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house);
        initView();
        getTabList();

        // 初始化 RecyclerView 并绑定默认数据
        houseBean = new HouseBean(); // 初始化 houseBean
        bindRecyList(houseBean);

        // 初始显示 "二手" 数据
        Searchhousing("二手",null);
        house_search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String tabtext = tabList.getTabAt(tabList.getSelectedTabPosition()).getText().toString();
                Searchhousing(tabtext, query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // 当搜索框文本发生变化时触发，可以实现实时搜索
                if (TextUtils.isEmpty(newText)) {
                    // 搜索文本为空时，还原适配器的数据为原来的数据
                    String tabtext = tabList.getTabAt(tabList.getSelectedTabPosition()).getText().toString();
                    Searchhousing(tabtext, null);
                }
                return false;
            }
        });


}

    private void getTabList() {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("二手");
        arrayList.add("租房");
        arrayList.add("楼盘");
        arrayList.add("中介");

        for (String category : arrayList) {
            TabLayout.Tab tab = tabList.newTab();
            tab.setText(category);
            tabList.addTab(tab);
        }

        tabList.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String tabtext = tab.getText().toString();
                Searchhousing(tabtext, house_search.getQuery().toString());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

    }

    private void Searchhousing(String tabtext, String searchText) {
        String url = "prod-api/api/house/housing/list?houseType=" + tabtext;
        if (!TextUtils.isEmpty(searchText)) {
            url += "&sourceName=" + searchText;
        }

        OKHttpTool.doNetRequest(url, OKHttpTool.GET, HouseBean.class, new OKHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                houseBean = (HouseBean) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bindRecyList(houseBean);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {
                // 处理请求失败情况
            }
        });
    }

    private void bindRecyList(HouseBean houseBean) {
        if (houseBean != null && houseBean.getRows() != null) {
            searchhousingAdapter = new SearchhousingAdapter(this, houseBean.getRows());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            rcvList.setLayoutManager(linearLayoutManager);
            rcvList.setAdapter(searchhousingAdapter);
        }
    }

    private void initView() {
        back = findViewById(R.id.back);
        house_search = findViewById(R.id.house_search);
        butHouse = findViewById(R.id.but_house);
        tabList = findViewById(R.id.tab_list);
        rcvList = findViewById(R.id.rcv_list);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HouseActivity.this.finish();
            }
        });
    }
}
